import { Node } from "cc";
import { IGameComponent, IGameEntity, IGameSystem } from "./interface/ecsEnterface";
import { HttpManager, Message, Network } from "../API";

export class GameComponent implements IGameComponent{
    private _owner:IGameEntity;
    private _node:Node;

    get NAME(): string { throw new Error("Method not implemented.");}
    get node(): Node {return this._node}
    get owner(): IGameEntity { return this._owner }

    private _list:IGameSystem[]=[]

    init(){

    }

    add<T extends IGameSystem>(system: new () => T) {
        let sys:IGameSystem=new system();
        sys["_owner"]=this
        sys["_node"]=this.node
        sys["init"] && sys["init"]()
        this._list.push(sys);
    }
    get(name: string): IGameSystem {
        for(let i=0;i<this._list.length;i++){
            if(this._list[i].NAME == name){
                return this._list[i]
            }
        }
        return null
    }
    remove(name: string) {
        for(let i=0;i<this._list?.length;i++){
            if(this._list[i].NAME == name){
                this._list[i].remove(this._list[i].NAME)
                this._list[i]=null
                this._list.splice(i,1)
                break
            } 
        }
    }
    removeAll(){ 
        for(let i=0;i<this._list?.length;i++){
            this._list[i].removeAll();
            this._list[i]=null;
            this._list.splice(i,1)
            i-- 
        }
    }
    update(dt:number){
        if(this._list){
            for(let i=0;i<this._list?.length;i++){
                this._list[i] && this._list[i].update(dt)
            }
        }
        
    }
    lateUpdate(dt:number){
        if(this._list){
            for(let i=0;i<this._list?.length;i++){
                this._list[i] && this._list[i].lateUpdate(dt)
            }
        }
    }

    /**
     * websocket 请求
     * @param msg       请求消息
     * @param data      请求数据
     * @param callback  请求完成回调
     * 
     */
    send(msg:string|number,data:any,callback?:()=>void){
        Network.getInstance().send(msg,data,callback)
    }
    /**
     * http请,默认是发生POST请求
     * @param path  url 请求地址
     * @param data  请求数据
     * @param type  请求类型
     * 
     */
    request(path:string,data:any,type?:string):Promise<any>{
        if(!type)type="POST" 
        if(type == "POST")return HttpManager.post(path,data);
        return HttpManager.get(path,data)
    }

    /**
     * 添加消息监听
     * @param msg 
     * @param callFun 
     * @param caller 
     */
    addMsg(msg:string,callFun:any,caller:any){
        Message.addGroup(this.NAME,msg,callFun,caller)
    }
    /**
     * 发送消息
     * @param msg 
     * @param params 
     */
    sendMsg(msg:string,...params:any){
        Message.sendGroup(this.NAME,msg,...params)
    }
    /**
     * 删除消息监听
     * @param msg 
     * @param callFun 
     * @param caller 
     */
    removeMsg(msg:string,callFun:any,caller:any){
        Message.removeGroupMsg(this.NAME,msg,callFun,caller)
    }
    /**
     * 删除当前组消息
     * @param group 
     */
    removeGroup(){
        Message.removeGroup(this.NAME)
    }
    /** 将消息添加到全局消息模块中 */
    addWorld(msg:string,callFun:any,caller:any){
        Message.add(msg,callFun,caller)
    }
    /** 发送全局消息 */
    sendWorld(msg:string,...params){
        Message.send(msg,...params)
    }
    /** 删除全局消息监听 */
    removeWorld(msg:string,callFun:any,caller:any){
        Message.remove(msg,callFun,caller)
    }
}